import { api_login } from '@/api/login'
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
export interface userStateType {
  userName: string
}
export const fetchUserInfo = createAsyncThunk(
  'user/fetchUserInfo',
  async (params) => {
    const { data } = await api_login(params)
    return data.data.name
  }
)
const userSlice = createSlice({
  // 模块名称独一无二
  name: 'user',
  // 初始数据
  initialState: {
    userName: '',
  },
  // 修改数据的同步方法
  reducers: {
    setUserName(state, actions) {
      state.userName = actions.payload
    },
  },
  extraReducers: (builder) => {
    builder.addCase(fetchUserInfo.fulfilled, (state, actions) => {
      state.userName = actions.payload
    })
  },
})

export const { setUserName } = userSlice.actions

//reducer函数
export default userSlice.reducer
